


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: MapConverter</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.convert.vienna.util</a> ]
</div>

<h1>Coverage Summary for Class: MapConverter (at.schauer.gregor.vienna.convert.vienna.util)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">MapConverter</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (7/ 7)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (33/ 33)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.convert.vienna.util;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.convert.vienna.base.AbstractContainerConverter;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.serializer.Serializer;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.util.ViennaUtils;
<i>21</i>&nbsp;import org.springframework.util.ClassUtils;
<i>22</i>&nbsp;
<i>23</i>&nbsp;import java.util.*;
<i>24</i>&nbsp;
<i>25</i>&nbsp;/**
<i>26</i>&nbsp; * @author Gregor Schauer
<i>27</i>&nbsp; */
<b class="fc"><i>28</i>&nbsp;public class MapConverter&lt;T extends Map&lt;K, V&gt;, K, V&gt; extends AbstractContainerConverter&lt;T, Map.Entry&lt;K, V&gt;&gt; {</b>
<i>29</i>&nbsp;	private static final String EQUALS = &quot;=&quot;;
<i>30</i>&nbsp;
<i>31</i>&nbsp;	public MapConverter() {
<b class="fc"><i>32</i>&nbsp;		super(&quot;{&quot;, &quot;}&quot;);</b>
<b class="fc"><i>33</i>&nbsp;	}</b>
<i>34</i>&nbsp;
<i>35</i>&nbsp;	@Override
<i>36</i>&nbsp;	public T sourceToTarget(String source, Class&lt;? extends T&gt; targetClass) {
<b class="fc"><i>37</i>&nbsp;		source = stripContainerSequences(source);</b>
<b class="fc"><i>38</i>&nbsp;		String[] split = split(source, String.format(&quot;[%s%s]&quot;, getSeparator(), EQUALS));</b>
<b class="fc"><i>39</i>&nbsp;		T instance = createInstance(targetClass, split.length);</b>
<i>40</i>&nbsp;
<b class="fc"><i>41</i>&nbsp;		for (int i = 0; i &lt; split.length; i++) {</b>
<b class="fc"><i>42</i>&nbsp;			add(instance, i, new AbstractMap.SimpleEntry&lt;K, V&gt;(this.&lt;K&gt;deserialize(split[i]), this.&lt;V&gt;deserialize(split[++i])));</b>
<i>43</i>&nbsp;		}
<b class="fc"><i>44</i>&nbsp;		return instance;</b>
<i>45</i>&nbsp;	}
<i>46</i>&nbsp;
<i>47</i>&nbsp;	@Override
<i>48</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>49</i>&nbsp;	public String targetToSource(T target) {
<b class="fc"><i>50</i>&nbsp;		StringBuilder builder = new StringBuilder();</b>
<b class="fc"><i>51</i>&nbsp;		appendLeadingSequence(builder);</b>
<i>52</i>&nbsp;
<b class="fc"><i>53</i>&nbsp;		boolean required = false;</b>
<b class="fc"><i>54</i>&nbsp;		List&lt;Integer&gt; separatorPositions = new ArrayList&lt;Integer&gt;();</b>
<b class="fc"><i>55</i>&nbsp;		for (Iterator&lt;Map.Entry&lt;K, V&gt;&gt; iterator = getElements(target); iterator.hasNext(); ) {</b>
<b class="fc"><i>56</i>&nbsp;			Map.Entry&lt;K, V&gt; elem = iterator.next();</b>
<i>57</i>&nbsp;
<i>58</i>&nbsp;			// Key
<b class="fc"><i>59</i>&nbsp;			Object identifier = ViennaUtils.SerializationUtils.save(ctx, elem.getKey());</b>
<b class="fc"><i>60</i>&nbsp;			String str = ClassUtils.getQualifiedName(elem.getKey().getClass()) + Serializer.CLASS_OBJECT_SEPARATOR + identifier;</b>
<b class="fc"><i>61</i>&nbsp;			builder.append(str).append(EQUALS);</b>
<i>62</i>&nbsp;
<b class="fc"><i>63</i>&nbsp;			separatorPositions.add(str.length());</b>
<b class="fc"><i>64</i>&nbsp;			required = required || requireIndices(str);</b>
<i>65</i>&nbsp;
<i>66</i>&nbsp;			// Value
<b class="fc"><i>67</i>&nbsp;			identifier = ViennaUtils.SerializationUtils.save(ctx, elem.getValue());</b>
<b class="fc"><i>68</i>&nbsp;			str = ClassUtils.getQualifiedName(elem.getValue().getClass()) + Serializer.CLASS_OBJECT_SEPARATOR + identifier;</b>
<b class="fc"><i>69</i>&nbsp;			builder.append(str).append(getSeparator());</b>
<i>70</i>&nbsp;
<b class="fc"><i>71</i>&nbsp;			separatorPositions.add(str.length());</b>
<b class="fc"><i>72</i>&nbsp;			required = required || requireIndices(str);</b>
<b class="fc"><i>73</i>&nbsp;		}</b>
<i>74</i>&nbsp;
<b class="fc"><i>75</i>&nbsp;		if (separatorPositions.size() &gt; 1 &amp;&amp; required) {</b>
<b class="fc"><i>76</i>&nbsp;			builder.insert(0, separatorPositions.toString().replace(&quot; &quot;, &quot;&quot;));</b>
<i>77</i>&nbsp;		}
<b class="fc"><i>78</i>&nbsp;		return appendTrailingSequence(builder).toString();</b>
<i>79</i>&nbsp;	}
<i>80</i>&nbsp;
<i>81</i>&nbsp;	@Override
<i>82</i>&nbsp;	protected void add(T instance, int i, Map.Entry&lt;K, V&gt; elem) {
<b class="fc"><i>83</i>&nbsp;		instance.put(elem.getKey(), elem.getValue());</b>
<b class="fc"><i>84</i>&nbsp;	}</b>
<i>85</i>&nbsp;
<i>86</i>&nbsp;	@Override
<i>87</i>&nbsp;	protected boolean requireIndices(String str) {
<b class="fc"><i>88</i>&nbsp;		return super.requireIndices(str) || str.contains(EQUALS);</b>
<i>89</i>&nbsp;	}
<i>90</i>&nbsp;
<i>91</i>&nbsp;	@Override
<i>92</i>&nbsp;	protected Iterator&lt;Map.Entry&lt;K, V&gt;&gt; getElements(T target) {
<b class="fc"><i>93</i>&nbsp;		return target.entrySet().iterator();</b>
<i>94</i>&nbsp;	}
<i>95</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:22</div>
</div>
</body>
</html>
